import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

public class Demo20 {
    public static void main(String[] args) {
        // 搞一个阻塞队列, 作为交易场所
        BlockingDeque<Integer> blockingDeque = new LinkedBlockingDeque<>();

        //生产者
        Thread thread1 = new Thread(() -> {
            int count = 0;
            while (true) {
                try {
                    System.out.println("生产元素: " + count);
                    blockingDeque.put(count);
                    ++count;
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                   e.printStackTrace();
                }
            }
        });

        //消费者
        Thread thread2 = new Thread(() -> {
            while (true) {
                try {
                    Integer number = blockingDeque.take();
                    System.out.println("消费元素: " + number);
                } catch (InterruptedException e) {
                   e.printStackTrace();
                }
            }
        });

        thread1.start();
        thread2.start();
    }
}
